package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.lawEnforcing.impl.dao.FarmHygieneExaminePigDao;
import com.jiantong.lawEnforcing.model.FarmHygieneExaminePig;
import com.jiantong.lawEnforcing.service.FarmHygieneExaminePigService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 动物养殖场动物卫生监督检查情况表
 * Created by mz03 on 2017/9/29.
 */
@Slf4j
@Service
@RpcProvider
public class FarmHygieneExaminePigServiceImpl implements FarmHygieneExaminePigService {

    private final FarmHygieneExaminePigDao farmHygieneExaminePigDao;

    @Autowired
    public FarmHygieneExaminePigServiceImpl(FarmHygieneExaminePigDao farmHygieneExaminePigDao){
        this.farmHygieneExaminePigDao = farmHygieneExaminePigDao;
    }

    /**
     * id 查找
     *
     * @param id
     * @return FarmHygieneExamine
     */
    @Override
    public Response<FarmHygieneExaminePig> finById(Long id) {
        try {
            FarmHygieneExaminePig farmHygieneExaminePig = farmHygieneExaminePigDao.findById(id);
            if (farmHygieneExaminePig == null) {
                log.error("farmHygieneExaminePig (id={}) is not found", id);
                return Response.fail("根据id查询执法督察信息失败");
            }
            return Response.ok(farmHygieneExaminePig);
        } catch (Exception e) {
            log.error("failed to find farmHygieneExaminePig(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("farmHygieneExaminePig.find.fail");
        }
    }

    /**
     * 创建
     *
     * @param farmHygieneExaminePig 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(FarmHygieneExaminePig farmHygieneExaminePig) {
        try {
            farmHygieneExaminePigDao.create(farmHygieneExaminePig);
            return Response.ok(farmHygieneExaminePig.getId());
        } catch (Exception e) {
            log.error("failed create farmHygieneExaminePig cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed create farmHygieneExaminePig");
        }
    }

    /**
     * 更新
     *
     * @param farmHygieneExaminePig 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(FarmHygieneExaminePig farmHygieneExaminePig) {
        try {
            return Response.ok(farmHygieneExaminePigDao.update(farmHygieneExaminePig));
        } catch (Exception e) {
            log.error("update farmHygieneExaminePig failed cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("更新执法督察(猪)单据失败");
        }
    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param criteria
     * @return
     */
    @Override
    public Response<Paging<FarmHygieneExaminePig>> pagingByStatus(Integer pageNo, Integer pageSize, Map<String, Object> criteria) {
        try{
            return Response.ok(farmHygieneExaminePigDao.paging(pageNo,pageSize,criteria));
        }catch (Exception e){
            log.error("failed to find paging farmHygieneExaminePig, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("paging farmHygieneExaminePig.find.fail");
        }
    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param criteria 此时参数为 FarmHygieneExamine 对象
     * @return
     */
    @Override
    public Response<Paging<FarmHygieneExaminePig>> paging(Integer pageNo, Integer pageSize, FarmHygieneExaminePig criteria) {
        try{
            return Response.ok(farmHygieneExaminePigDao.paging(pageNo,pageSize,criteria));
        }catch (Exception e){
            log.error("failed to find paging farmHygieneExaminePig, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("paging farmHygieneExaminePig.find.fail");
        }
    }


}
